﻿
var gPluginPath=external.m2_plugin_folder(SECURITY_ID,PLUGIN_NAME);

var gLastClickSignID=1;
var configNames=["insertAtCaret","hideAfterInsert","showNumber","showTempGroup"];
var configNames2=["X","Y"];
$(function(){
	for(var i=0;i<configNames.length;i++)
	{
		document.getElementById(configNames[i]).checked=pharaoh_Config[configNames[i]];
	}
	for(var i=0;i<configNames2.length;i++)
	{
		document.getElementById(configNames2[i]).value=pharaoh_Config[configNames2[i]];
	}	
	
	var autorunUrl=external.readFile(SECURITY_ID, PLUGIN_NAME,"userData\\autorunUrl.lst");
	if(autorunUrl)
	{
		autorunUrl=clear0(autorunUrl);
		document.getElementById("autorunUrls").value=autorunUrl;
	}
	else
	{
		document.getElementById("autorunUrls").value="";
	}
	if(external.m2_readIni(SECURITY_ID,PLUGIN_NAME,"plugin.ini", "General", "StartAfterPageDone", 0)==1)
	{
		document.getElementById("isAutoRun").checked=true;	
	}
	else
	{
		document.getElementById("isAutoRun").checked=false;		
	}
	
		
	var fso=external.max_activex(SECURITY_ID,"Scripting.FileSystemObject");
	document.getElementById("userTmpPath").checked=fso.FileExists(gPluginPath+"tool\\settmp.bat");

	fso=null;
});

function AutoRunChanged()
{
	//var val="0";
	//if(document.getElementById("isAutoRun").checked){val="1";}
	var oPluginManager=external.max_getObj(SECURITY_ID, 'PluginManager');
	var length=oPluginManager.getCount();
	var thisPlugin=null;
	for(var i=0;i<length;i++)
	{
		var plg=oPluginManager.getPlugin(i);
		if(plg.title==PLUGIN_NAME)
		{
			plg.startAfterPageDone=document.getElementById("isAutoRun").checked;
			break;
		}
	}

}

function userTmpPathChanged()
{
	var fso=external.max_activex(SECURITY_ID,"Scripting.FileSystemObject");
	if(document.getElementById("userTmpPath").checked)
	{
		fso.CopyFile(gPluginPath+"tool\\settmp.txt",gPluginPath+"tool\\settmp.bat");
	}
	else
	{
		fso.DeleteFile(gPluginPath+"tool\\settmp.bat",true);
	}
	fso=null;
}


function UrlTextChanged()
{
	var runUrls=trim(document.getElementById("autorunUrls").value);
	var saveText="";

	if(runUrls)
	{
		var runs=runUrls.split("\n");
		var save=[];
		for(var i=0;i<runs.length;i++)
		{
			var txt=trim(runs[i]);
			if(txt)
			{
				save.push(txt);
			}
		}
		if(save.length>0)
		{
			saveText=save.join("\r\n");
		}
	}
	//alert(saveText);
	external.writeFile(SECURITY_ID, PLUGIN_NAME,"userData\\autorunUrl.lst",saveText);
}

function checkXYInput(id,min,max)
{
	var v=trim(document.getElementById(id).value);
	var iv=parseInt(v,10);
	if(isNaN(iv))
	{
		alert("请输入正整数！");
		event.returnValue=false;		
		return -1;
	}
	if((iv<min)||(iv>max))
	{
		alert("请输入["+min+"~"+max+"]之间的正整数！");
		event.returnValue=false;
		return -1;
	}
	return iv;
}

function ConfigChanged()
{
	var iX = checkXYInput("X",8,20);
	var iY=checkXYInput("Y",3,10);
	
	if((iX==-1)||(iY==-1))
	{
		return;
	}
	
	var cfgText=[];
	for(var i=0;i<configNames.length;i++)
	{
		var isCheck=document.getElementById(configNames[i]).checked;
		pharaoh_Config[configNames[i]]=isCheck;
		cfgText.push("pharaoh_Config."+configNames[i]+"="+isCheck+";");
	}
	for(var i=0;i<configNames2.length;i++)
	{
		var isCheck=document.getElementById(configNames2[i]).value;
		pharaoh_Config[configNames2[i]]=isCheck;
		cfgText.push("pharaoh_Config."+configNames2[i]+"="+isCheck+";");
	}	
//	alert(cfgText.join("\r\n"));
	external.writeFile(SECURITY_ID,PLUGIN_NAME,"userData\\config.js",cfgText.join("\r\n"));
}

function printobj(o)
{
	var msg='';
	for(var k in o)
	{
		msg+=k+"\t:"+o[k]+"\n";
	}
	alert(msg);
}